<?php
	include('db_connection.php');
	class User_affiliate{
	
		function saveSubscribeInfo(){		
			$count = 0;
			$reference_value = $this->createRandomStringForReference();

			//echo '<pre>'; print_r($_POST); echo '</pre>'; exit;
			
			$select = "SELECT * FROM tbl_affiliate WHERE email_id='".$_POST['aff_user_email']."'";	
			$query = mysql_query($select);
			$row = mysql_num_rows ( $query );
			$msg = "";
			if($row>0){
				$msg = "This email is already exist in our system";
			}else{
				$ins_aff = "INSERT INTO tbl_affiliate (username,email_id,phone,city,address,state,zip,country,website_url,reference_url) VALUES ('".$_POST['aff_user_name']."','".$_POST['aff_user_email']."','".$_POST['user_phone']."','".$_POST['user_city']."','".$_POST['address']."','".$_POST['user_state']."','".$_POST['user_zip']."','".$_POST['user_country']."','".$_POST['web_url']."','".$reference_value."')";
				$query = mysql_query($ins_aff) or die(mysql_error());
				
				/* This Code is used for send Email. Start Here*/
					$mail_to=$_POST['aff_user_email'];
					$mail_subject="Affilation Account Created";
					$mail_body = "Your Account Is created.After checking you website admin will activate your account. .<br/>\n";
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
					$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
					
					$mail_to='Vipin7477@gmail.com';
					$mail_subject="Affilation Account Created";
					$mail_body = "Your Account Is created.After checking you website admin will activate your account. .<br/>\n";
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
					$sent = mail($mail_to,$mail_subject,$mail_body, $headers);	
										
				/* This Code is used for send Email. End Here*/	
				$msg = "You Account is created. After checking you website admin will activate your account.";
			}
			return $msg;
		}

		function createRandomStringForReference(){
			$random_string_final = '';
			$pwd_row = '';
			$alpha_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
			$numeric_arr = array('0','1','2','3','4','5','6','7','8','9');		
			
			$count = 1;
			while($count<2)
			{
				$random_alpha_string = $this->array_random_assoc($alpha_arr,5);
				$random_numeric_string = $this->array_random_assoc($numeric_arr,5);
				$random_string = $random_alpha_string.$random_numeric_string;
				$random_string = str_shuffle($random_string);		
				

				$select_user = "SELECT reference_url FROM tbl_affiliate WHERE reference_url='".$random_string."'";
				$query_user = mysql_query($select_user);
				$row_user = mysql_num_rows ( $query_user );
				
				if(!$row_user){					
					$count = 2;
					$random_string_final = $random_string;
				}
			}

			if($count == 2){
				return $random_string_final;
			}
		}

		function array_random_assoc($arr, $num = 1) {
			$keys = array_keys($arr);
			shuffle($keys);
			
			$r = '';
			for ($i = 0; $i < $num; $i++) {
				$r .= $arr[$keys[$i]];
			}
			return $r;
		}
	}
?>